<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * 用户表的列表信息
 * @author chenhuan
 *
 */
class Userborrow extends MY_Controller {

    function __construct() {
        parent::__construct();
        /** 登陆验证 */
        $user = $this->session->userdata('zjy_user');
        if (!isset($user['user_id'])) {
            $str = $_SERVER['REQUEST_URI'];
            $this->js->go('/login.html');
        } elseif (!$user['user_id']) {
            $this->js->go('/login.html');
        }

        $this->tab = 'user';
        $this->sub = 'borrow';
    }

    /**
     * 成功投资的借款标
     */
    function success() {

        $per_page = $this->input->get('per_page') ? $this->input->get('per_page') : 0;
        $user = $this->session->userdata('zjy_user');
        $data['where']['user_id'] = $user['user_id'];
        $data['where']['fstatus'] = 1;
        $data['page_size'] = USER_CENTER_PAGE_SIZE;
        $data['page_start'] = (int) $per_page;

        $this->load->model('Borrow_model');
        $vdata = $this->get_user_info('tender_success', 'success', 'tender');

        $tender_vdata = $this->Borrow_model->borrow_tender_user_list($data);
        $vdata['tender_list'] = $tender_vdata['tender_list'];

        //分页
        $this->load->library('pagination');
        $config['base_url'] = "/userborrow/success.html?";
        $config['total_rows'] = $tender_vdata['tender_count'];
        $config['per_page'] = USER_CENTER_PAGE_SIZE;
        $this->pagination->initialize($config);

        $vdata['type'] = 1;
        $vdata['title'] = "成功投资的借款标" . ' - ' . CON_KEYWORDS;
        $vdata['tab_first'] = 'invest';
        $vdata['tab_second'] = 'invest_mine';
        $this->load->view('/user/user_tender_success', $vdata);

        /*
          $per_page 			= $this->input->get('per_page')?$this->input->get('per_page'):0;

          $user = $this->session->userdata('zjy_user');
          $data['where']['user_id'] 	= $user['user_id'];
          //$data['where']['fstatus'] 	= -1;
          $data['select'] 	= 1;
          $data['order'] 		= 3;
          $data['page_size']  = USER_CENTER_PAGE_SIZE;
          $data['page_start'] = (int)$per_page;
          $this->load->model('Borrow_model');
          $vdata = $this->Borrow_model->get_user_collection($data);
          //分页
          $this->load->library('pagination');
          $config['base_url'] 	= "/userborrow/success/index.html?";
          $config['total_rows'] 	= $vdata['collection_count'];
          $config['per_page'] 	= USER_CENTER_PAGE_SIZE;
          $this->pagination->initialize($config);

          //投标总额
          $vdata['tender_total'] = $this->Borrow_model->tender_total($user['user_id']);
          //已收总额
          $collection_list = $this->Borrow_model->collection_total($user['user_id'], 1);
          //已收本金
          $vdata['ycollection_total'] = $collection_list['collection_total'];
          //已收本金
          $vdata['ycollection_capital'] = $collection_list['collection_capital'];
          //已收利息
          $vdata['ycollection_interest'] = $collection_list['collection_interest'];

          //已收总额
          $collection_list = $this->Borrow_model->collection_total($user['user_id'], 0);
          //未收总额
          $vdata['wcollection_total'] = $collection_list['collection_total_account'];
          //未收本金
          $vdata['wcollection_capital'] = $collection_list['collection_capital'];
          //未收利息
          $vdata['wcollection_interest'] = $collection_list['collection_interest'];

          $vdata['type'] = 1;
          $vdata['title'] 						= "成功投资的借款标".' - '.CON_KEYWORDS;
          $this->load->view('/user/user_borrow_success', $vdata);
         */
    }

    /**
     * 代收明细
     */
    function coll() {
        $act = $this->input->get('act');
        if ($act == 'excel') {
            $this->collexcel();
        } else {
            $per_page = $this->input->get('per_page') ? $this->input->get('per_page') : 0;
            $user = $this->session->userdata('zjy_user');
            $data['where']['user_id'] = $user['user_id'];
            $data['where']['fstatus'] = 0;
            $data['where']['strtime'] = $this->input->get('strtime') ? $this->input->get('strtime') : '';
            $data['where']['endtime'] = $this->input->get('endtime') ? $this->input->get('endtime') : '';
            $data['select'] = 1;
            $data['order'] = 1;
            $data['page_size'] = USER_CENTER_PAGE_SIZE;
            $data['page_start'] = (int) $per_page;
            $this->load->model('Borrow_model');

            $vdata = $this->get_user_info('tender_coll', 'success', 'tender');

            $coll_vdata = $this->Borrow_model->get_user_collection($data);
            $vdata['collection_list'] = $coll_vdata['collection_list'];
            //分页
            $this->load->library('pagination');
            $config['base_url'] = "/userborrow/coll.html?strtime=" . $data['where']['strtime'] . "&endtime=" . $data['where']['endtime'];
            $config['total_rows'] = $coll_vdata['collection_count'];
            $config['per_page'] = USER_CENTER_PAGE_SIZE;
            $this->pagination->initialize($config);
            $vdata['type'] = 3;
            $vdata['title'] = "未收款明细" . ' - ' . CON_KEYWORDS;
	        $vdata['tab_first'] = 'invest';
	        $vdata['tab_second'] = 'invest_mine';
            $this->load->view('/user/user_borrow_coll', $vdata);
        }
    }

    function collexcel() {
        $user = $this->session->userdata('zjy_user');
        $data['where']['user_id'] = $user['user_id'];
        $data['where']['fstatus'] = 0;
        $data['where']['strtime'] = $this->input->get('strtime') ? $this->input->get('strtime') : '';
        $data['where']['endtime'] = $this->input->get('endtime') ? $this->input->get('endtime') : '';
        $data['select'] = 1;
        $data['order'] = 1;
        $data['limit'] = 'all';
        $this->load->model('Borrow_model');
        $list = $this->Borrow_model->get_user_collection($data);

        $this->load->library('PHPExcel');
        $this->load->library('PHPExcel/IOFactory');
        $objPHPExcel = new PHPExcel();
        $objPHPExcel->getProperties()->setTitle("待收明细")->setDescription(COMPANY_NAME);
        $objPHPExcel->setActiveSheetIndex(0);
        $titles = array("借款标", "期数", "应收时间", "应收本金", "应收利息");

        for ($i = 0; $i < count($titles); $i ++) {
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($i, 1, $titles[$i]);
        }

        for ($i = 0; $i < count($list); $i++) {
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(0, $i + 2, $list[$i]['title']);
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(1, $i + 2, $list[$i]['time_limit']);
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(2, $i + 2, date('Y-m-d', $list[$i]['repay_time']));
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(3, $i + 2, $list[$i]['capital']);
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(4, $i + 2, $list[$i]['interest']);
        }

        $objWriter = IOFactory::createWriter($objPHPExcel, 'Excel5');
        //发送标题强制用户下载文件
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="待收明细.xls"');
        header('Cache-Control: max-age=0');
        $objWriter->save('php://output');
    }

    /**
     * 已收明细
     */
    function ycoll() {
        $per_page = $this->input->get('per_page') ? $this->input->get('per_page') : 0;

        $user = $this->session->userdata('zjy_user');
        $data['where']['user_id'] = $user['user_id'];
        $data['where']['fstatus'] = 1;
        $data['select'] = 1;
        $data['order'] = 4;
        $data['page_size'] = USER_CENTER_PAGE_SIZE;
        $data['page_start'] = (int) $per_page;
        $this->load->model('Borrow_model');
        $vdata = $this->get_user_info('tender_ycoll', 'success', 'tender');

        $ycoll_vdata = $this->Borrow_model->get_user_collection($data);
        $vdata['collection_list'] = $ycoll_vdata['collection_list'];

        //分页
        $this->load->library('pagination');
        $config['base_url'] = "/userborrow/ycoll.html?";
        $config['total_rows'] = $ycoll_vdata['collection_count'];
        $config['per_page'] = USER_CENTER_PAGE_SIZE;
        $this->pagination->initialize($config);
        $vdata['type'] = 4;
        $vdata['title'] = "已收明细" . ' - ' . CON_KEYWORDS;
	    $vdata['tab_first'] = 'invest';
	    $vdata['tab_second'] = 'invest_mine';
        $this->load->view('/user/user_borrow_coll', $vdata);
    }

    /**
     * 正在投资的借款标
     */
    /* function now(){
      $per_page 			= $this->input->get('per_page')?$this->input->get('per_page'):0;

      $user = $this->session->userdata('zjy_user');
      $data['where']['user_id'] 	= $user['user_id'];
      $data['where']['fstatus'] 	= 0;
      $data['select'] 	= 1;
      $data['page_size']  = BORROW_LIST_PAGE_SIZE;
      $data['page_start'] = (int)$per_page;
      $this->load->model('Borrow_model');
      $vdata = $this->Borrow_model->get_user_collection($data);
      //分页
      $this->load->library('pagination');
      $config['base_url'] = "/userborrow/now/index.html?";
      $config['total_rows'] = $vdata['collection_count'];
      $config['per_page'] = BORROW_LIST_PAGE_SIZE;
      $config['first_link'] = '首页';
      $config['last_link'] = '尾页';
      $config['next_link'] = '下一页';
      $config['prev_link'] = '上一页';
      $this->pagination->initialize($config);
      $vdata['type'] = 2;
      $this->load->view('/user/user_borrow_success', $vdata);
      } */

    /**
     * 正在投资的借款标
     */
    function now() {
        $per_page = $this->input->get('per_page') ? $this->input->get('per_page') : 0;

        $user = $this->session->userdata('zjy_user');
        $data['where']['user_id'] = $user['user_id'];
        $data['where']['fstatus'] = 1;
        $data['select'] = 1;
        $data['page_size'] = USER_CENTER_PAGE_SIZE;
        $data['page_start'] = (int) $per_page;
        $this->load->model('Borrow_model');

        $vdata = $this->get_user_info('tender_now', 'success', 'tender');

        $now_vdata = $this->Borrow_model->get_borrow_tender_lists($data);
        $vdata['tender_list'] = $now_vdata['tender_list'];

        //分页
        $this->load->library('pagination');
        $config['base_url'] = "/userborrow/now/index.html?";
        $config['total_rows'] = $now_vdata['tender_count'];
        $config['per_page'] = USER_CENTER_PAGE_SIZE;
        $this->pagination->initialize($config);
        $vdata['type'] = 2;
        $vdata['title'] = "正在投标的借款" . ' - ' . CON_KEYWORDS;
	    $vdata['tab_first'] = 'invest';
	    $vdata['tab_second'] = 'invest_mine';
        $this->load->view('/user/user_borrow_now', $vdata);
    }

    function publish() {
        $per_page = $this->input->get('per_page') ? $this->input->get('per_page') : 0;

        $user = $this->session->userdata('zjy_user');
        $data['where']['user_id'] = $user['user_id'];
        $data['where']['fstatus'] = 1;
        $data['select'] = 1;
        $data['page_size'] = USER_CENTER_PAGE_SIZE;
        $data['page_start'] = (int) $per_page;
        $this->load->model('Borrow_model');
        $vdata = $this->get_user_info('borrow_publish', 'publish');
        $borrow_vdata = $this->Borrow_model->get_user_borrow($data);
        $vdata['borrow_list'] = $borrow_vdata['borrow_list'];

        //分页
        $this->load->library('pagination');
        $config['base_url'] = "/userborrow/publish/index.html?";
        $config['total_rows'] = $vdata['borrow_count'];
        $config['per_page'] = USER_CENTER_PAGE_SIZE;
        $this->pagination->initialize($config);
        $vdata['borrow_type'] = $this->Borrow_model->arrays('borrow_type'); //标种
        $vdata['type'] = 1;
        $vdata['title'] = "正在招标的借款" . ' - ' . CON_KEYWORDS;
        $vdata['tab_first'] = 'borrow';
        $vdata['tab_second'] = 'borrow_mine';
        $this->load->view('/user/user_loan_borrow', $vdata);
    }

    function unpublish() {
        $per_page = $this->input->get('per_page') ? $this->input->get('per_page') : 0;

        $user = $this->session->userdata('zjy_user');
        $data['where']['user_id'] = $user['user_id'];
        $data['where']['fstatus'] = 0;
        $data['select'] = 1;
        $data['page_size'] = USER_CENTER_PAGE_SIZE;
        $data['page_start'] = $per_page;
        $this->load->model('Borrow_model');
        $vdata = $this->get_user_info('borrow_unpublish', 'publish');

        $b_vdata = $this->Borrow_model->get_user_borrow($data);
        $vdata['borrow_list'] = $b_vdata['borrow_list'];
        //分页
        $this->load->library('pagination');
        $config['base_url'] = "/userborrow/unpublish/index.html?";
        $config['total_rows'] = $b_vdata['borrow_count'];
        $config['per_page'] = USER_CENTER_PAGE_SIZE;
        $this->pagination->initialize($config);
        $vdata['borrow_type'] = $this->Borrow_model->arrays('borrow_type'); //标种
        $vdata['type'] = 2;
        $vdata['title'] = "尚未发布的借款" . ' - ' . CON_KEYWORDS;
        $vdata['tab_first'] = 'borrow';
        $vdata['tab_second'] = 'borrow_mine';
        $this->load->view('/user/user_loan_borrow', $vdata);
    }

    function repayment() {
        $per_page = $this->input->get('per_page') ? $this->input->get('per_page') : 0;

        $user = $this->session->userdata('zjy_user');
        $data['where']['user_id'] = $user['user_id'];
        $data['where']['fstatus'] = 0;
        $data['select'] = 1;
        $data['order'] = 'asc';
        $data['page_size'] = USER_CENTER_PAGE_SIZE;
        $data['page_start'] = (int) $per_page;
        $this->load->model('Borrow_model');
        $vdata = $this->get_user_info('borrow_repayment', 'publish');

        $r_vdata = $this->Borrow_model->get_repayment_list($data);
        $vdata['repayment_list'] = $r_vdata['repayment_list'];

        if (count($vdata['repayment_list']) < 1) {
            $data['where']['ftype'] = 4;
            $v1data = $this->Borrow_model->get_lz_repayment_list($data);
            if (count($v1data['repayment_list']) > 0) {
                header("location:/userborrow/lzrepayment/index.html");
                exit;
            }
        }
        //分页
        $this->load->library('pagination');
        $config['base_url'] = "/userborrow/repayment/index.html?";
        $config['total_rows'] = $r_vdata['repayment_count'];
        $config['per_page'] = USER_CENTER_PAGE_SIZE;
        $this->pagination->initialize($config);
        $vdata['borrow_type'] = $this->Borrow_model->arrays('borrow_type'); //标种
        $vdata['type'] = 3;
        $vdata['title'] = "正在还款的借款" . ' - ' . CON_KEYWORDS;
        $vdata['tab_first'] = 'borrow';
        $vdata['tab_second'] = 'borrow_mine';
        $this->load->view('/user/user_loan_repayment', $vdata);
    }

    /**
     * 流转标还款列表
     */
    function lzrepayment() {
        $per_page = $this->input->get('per_page') ? $this->input->get('per_page') : 0;

        $user = $this->session->userdata('zjy_user');
        $data['where']['user_id'] = $user['user_id'];
        $data['where']['fstatus'] = 0;
        $data['where']['ftype'] = 4;
        $data['select'] = 1;
        $data['order'] = '1';
        $data['page_size'] = USER_CENTER_PAGE_SIZE;
        $data['page_start'] = (int) $per_page;
        $this->load->model('Borrow_model');
        $vdata = $this->Borrow_model->get_lz_repayment_list($data);
        //分页
        $this->load->library('pagination');
        $config['base_url'] = "/userborrow/lzrepayment/index.html?";
        $config['total_rows'] = $vdata['repayment_count'];
        $config['per_page'] = USER_CENTER_PAGE_SIZE;
        $this->pagination->initialize($config);
        $vdata['borrow_type'] = $this->Borrow_model->arrays('borrow_type'); //标种
        $vdata['type'] = 5;
        $vdata['title'] = "正在还款的流转标" . ' - ' . CON_KEYWORDS;
        $this->load->view('/user/user_loan_repayment', $vdata);
    }

    /**
     * 已还完的借款标
     */
    function over() {
        $per_page = $this->input->get('per_page') ? $this->input->get('per_page') : 0;

        $user = $this->session->userdata('zjy_user');
        $data['where']['user_id'] = $user['user_id'];
        $data['where']['fstatus'] = 3;
        $data['where']['fstatus1'] = 'over';
        $data['select'] = 1;
        $data['page_size'] = USER_CENTER_PAGE_SIZE;
        $data['page_start'] = (int) $per_page;
        $this->load->model('Borrow_model');
        $vdata = $this->get_user_info('borrow_over', 'publish');

        $b_vdata = $this->Borrow_model->get_user_borrow($data);
        $vdata['borrow_list'] = $b_vdata['borrow_list'];

        //分页
        $this->load->library('pagination');
        $config['base_url'] = "/userborrow/over/index.html?";
        $config['total_rows'] = $b_vdata['borrow_count'];
        $config['per_page'] = USER_CENTER_PAGE_SIZE;
        $this->pagination->initialize($config);
        $vdata['borrow_type'] = $this->Borrow_model->arrays('borrow_type'); //标种
        $vdata['type'] = 4;
        $vdata['title'] = "已还完的借款" . ' - ' . CON_KEYWORDS;
        $vdata['tab_first'] = 'borrow';
        $vdata['tab_second'] = 'borrow_mine';
        $this->load->view('/user/user_loan_borrow', $vdata);
    }

    function yjb() {
        $user = $this->session->userdata('zjy_user');
        $fstatus = intval($this->input->get('state'));
        $this->load->model('Yjb_model');
        $data['per_page'] = intval($this->input->get('per_page'));
        $data['page_size'] = 10;
        $data['fstatus'] = $fstatus;
        $data['user_id'] = $user['user_id'];
        if ($fstatus == 0) {
            $data['order'] = 'add_time';
        } elseif ($fstatus == 1) {
            $data['order'] = 'repay_time';
        } elseif ($fstatus == 3) {
            $data['order'] = 'add_time';
        }

        $tab_arr = array(0 => 'yjb_success', 1 => 'yjb_coll', 3 => 'yjb_ycoll');
        $vdata = $this->get_user_info($tab_arr[$fstatus], 'yjb', 'tender');


        $vdata_list = $this->Yjb_model->tender_list($data);
        $vdata['list'] = $vdata_list['list'];
        $vdata['total_money'] = $vdata_list['total_money'];
        $vdata['total_repay'] = $vdata_list['total_repay'];
        $vdata['total_people'] = $vdata_list['total_people'];

        $this->load->library('pagination');
        $config['base_url'] = '/userborrow/yjb.html?state=' . $fstatus;
        $config['total_rows'] = $vdata_list['count'];
        $config['per_page'] = YTZ_LIST_PAGE_SIZE;
        $this->pagination->initialize($config);
        $vdata['state'] = $fstatus;
        $vdata['title'] = "易居宝投资明细" . ' - ' . CON_KEYWORDS;

        //var_dump($vdata);exit;
        $this->load->view('/user/yjb_list', $vdata);
    }

    function yjbborrow() {
        $user = $this->session->userdata('zjy_user');
        $this->load->model('Yjb_model');
        $data['where']['user_id'] = $user['user_id'];
        $data['per_page'] = intval($this->input->get('per_page'));
        $data['page_size'] = YTZ_LIST_PAGE_SIZE;
        $ret = $this->Yjb_model->get_list($data);
        $this->load->library('pagination');
        $config['base_url'] = '/userborrow/yjb.html?pagesize=' . $data['page_size'];
        $config['total_rows'] = $ret['count'];
        $config['per_page'] = $data['page_size'];
        $this->pagination->initialize($config);
        $vdata['list'] = $ret['list'];
        $vdata['type'] = 6;
        $vdata['title'] = "易居宝借款列表" . ' - ' . CON_KEYWORDS;
        $this->load->view('/user/user_yjb_borrow', $vdata);
    }

    function yjbwithdraw($id) {
        $id = intval($id);
        $user = $this->session->userdata('zjy_user');
        $this->load->model('Yjb_model');
        $obj = $this->Yjb_model->get_by_id($id);
        if ($obj['user_id'] == $user['user_id'] && $obj['fstatus'] <= 1) {
            $setArr = array('fstatus' => 5);
            $ret = $this->Yjb_model->update($setArr, array('id' => $id));
            echo $ret ? 1 : 0;
            exit;
        }
        echo 0;
    }

    /**
     * 可转让的债权
     */
    function transfer() {
        $user = $this->session->userdata('zjy_user');
        $this->load->model('Creditor_model');
        $data['user_id'] = $user['user_id'];

        $vdata = $this->get_user_info('transfer', 'transferlog', 'tender');

        $list = $this->Creditor_model->get_mb_transfer_list($data);
        //var_dump($list);exit;
        $vdata['list'] = $list;
        $vdata['title'] = "可转让的债权" . ' - ' . CON_KEYWORDS;
        $vdata['tab_first'] = 'invest';
        $vdata['tab_second'] = 'invest_transfer';
        $this->load->view('/user/user_borrow_transfer', $vdata);
    }

    /**
     * 我发布的债权
     */
    function mycreditor() {
        $user = $this->session->userdata('zjy_user');
        $this->load->model('Creditor_model');
        $data['user_id'] = $user['user_id'];
        $data['per_page'] = intval($this->input->get('per_page'));
        $data['page_size'] = 10;
        $data['order'] = 'fstatus';

        $vdata = $this->get_user_info('mycredit', 'transferlog', 'tender');

        $vdata_list = $this->Creditor_model->transfer_obj_list($data);
        $vdata['list'] = $vdata_list['list'];
        //var_dump($vdata);exit;
        //分页
        $this->load->library('pagination');
        $config['base_url'] = '/userborrow/mycreditor.html?size=10';
        $config['total_rows'] = $vdata_list['count'];
        $config['per_page'] = $data['page_size'];
        $this->pagination->initialize($config);

        $vdata['title'] = "我发布的债权" . ' - ' . CON_KEYWORDS;
        $vdata['tab_first'] = 'invest';
        $vdata['tab_second'] = 'invest_transfer';
        $this->load->view('/user/user_borrow_mycreditor', $vdata);
    }

    /**
     * 我购买的债权
     */
    function transferlog() {
        $user = $this->session->userdata('zjy_user');
        $this->load->model('Creditor_model');
        $data['user_id'] = $user['user_id'];
        $data['per_page'] = intval($this->input->get('per_page'));
        $data['page_size'] = 10;

		$vdata = $this->get_user_info('mytran', 'transferlog', 'tender');

        $vdata_list = $this->Creditor_model->my_creditor_log($data);
        $vdata['list'] = $vdata_list['list'];
        //分页
        $this->load->library('pagination');
        $config['base_url'] = '/userborrow/transferlog.html?size=10';
        $config['total_rows'] = $vdata_list['count'];
        $config['per_page'] = $data['page_size'];
        $this->pagination->initialize($config);
		
        $vdata['title'] = "我购买的债权" . ' - ' . CON_KEYWORDS;
        $vdata['tab_first'] = 'invest';
        $vdata['tab_second'] = 'invest_transfer';
        $this->load->view('/user/user_borrow_transferlog', $vdata);
    }

    /**
     * 我的待收债权明细
     */
    function ctlog() {
        $user = $this->session->userdata('zjy_user');
        $data['per_page'] = intval($this->input->get('per_page'));
        $data['page_size'] = 10;
        $data['user_id'] = $user['user_id'];
        $this->load->model('Creditor_model');

        $vdata = $this->get_user_info('ctlog', 'transferlog', 'tender');

        $vdata_list = $this->Creditor_model->ctlog($data);
        $vdata['list'] = $vdata_list['list'];
        //分页
        $this->load->library('pagination');
        $config['base_url'] = '/userborrow/ctlog.html?size=10';
        $config['total_rows'] = $vdata_list['count'];
        $config['per_page'] = $data['page_size'];
        $this->pagination->initialize($config);
        $vdata['title'] = "债权待收明细" . ' - ' . CON_KEYWORDS;
        $vdata['type'] = 4;
        $vdata['tab_first'] = 'invest';
        $vdata['tab_second'] = 'invest_transfer';
        $this->load->view('/user/ctlog', $vdata);
    }

    /**
     * 申请债权转让
     */
    function dotransfer() {
        $user = $this->session->userdata('zjy_user');
        $this->load->model('Creditor_model');
        $this->load->model('Mydb_model');
        $data['title'] = trim($this->input->post('title'));
        $data['tender_id'] = intval($this->input->post('tender_id'));
        $date = strtotime('today', time());
        //$sql = "select id from zjy_creditor_obj where user_id={$user['user_id']} and fstatus<4 and add_time>={$date} limit 1";
        $sql = "select p1.id from zjy_creditor_obj as p1 left join zjy_borrow_tender as p2 on p1.tender_id=p2.id
				where p2.user_id={$user['user_id']} and p1.fstatus<4 and p1.add_time>={$date} limit 1";

        $row = $this->Mydb_model->query_sql($sql, 'row');
        /* if ($row){
          echo '一天只能发布一个债券转让';
          exit;
          } */




        $sql = "select t1.*, t2.ftype from zjy_borrow_tender as t1 left join zjy_borrow as t2 on t1.borrow_id = t2.id where t1.id={$data['tender_id']}";
        $row = $this->Mydb_model->query_sql($sql, 'row');
        if (!$row) {
            echo '数据错误';
            exit;
        }
        if ($row['user_id'] != $user['user_id']) {
            echo '数据错误';
            exit;
        }



        $ret = $this->Creditor_model->check_tender_id($data['tender_id']);
        if ($ret) {
            echo '该债权已转让';
            exit;
        }
        if ($data['title'] == '') {
            echo '请选择标题';
            exit;
        }


        $time = date('Y-m-d');

//        //可转让天数是否达到条件
//        $canDays = 0;
//        if ($row['ftype'] == 1) {
//            $canDays = ONE_FINANCIAL;
//        } elseif ($row['ftype'] == 2) {
//            $canDays = ONE_MORTGAGE;
//        } else {
//            $canDays = ONE_GUARANTEE;
//        }
//        //没有可转让天数不能转让   未到可转让天数也不能转让
//        if ($canDays <= 0 || $row['add_time'] > strtotime("- $canDays days", $date)) {
//            echo '未达到转让条件';
//            exit;
//        }

        if ($row['account'] < CREDITOR_LIMIT) {
            echo '债权转让最低金额为' . CREDITOR_LIMIT;
            exit;
        }


        $data['fstatus'] = 1;
        $apr = floatval($this->input->post('apr'));
        $data['discount_apr'] = round($apr, 2);
        if ($data['discount_apr'] < 0.5 || $data['discount_apr'] > 11.5) {
            echo '折让率必须为0.5%-11.5%之间';
            exit;
        }

        $discount_account = $row['account'] * (100 - $data['discount_apr']) / 100;
        $data['discount_account'] = round($discount_account, 2);
        $data['discount_yes'] = 0;
        $data['content'] = $this->input->post('content', true);
        $data['add_time'] = time();
        $data['valid_time'] = intval($this->input->post('valid_time'));

        if ($data['content'] == '') {
            echo '请填写说明';
            exit;
        }
        if (!$data['valid_time']) {
            echo '请选择有效期';
            exit;
        }
        //print_r($data);
        //exit;
        $ret = $this->Creditor_model->apply_transfer($data);
        if ($ret) {
            echo '申请成功，请等待审核';
        } else {
            echo '操作失败，请联系客服';
        }
    }

    /**
     * 取消申请
     */
    function applycancel() {
        $user = $this->session->userdata('zjy_user');
        $id = intval($this->input->get('id'));
        $fstatus = 4;
        $this->load->model('Creditor_model');
        $obj = $this->Creditor_model->get_obj_by_id($id);
        if ($id > 0 && $obj['fstatus'] == 1 && $user['user_id'] == $obj['user_id']) {
            $data = array(
                'id' => $id,
                'fstatus' => $fstatus,
                'verify_remark' => '用户取消',
                'tender_id' => $obj['tender_id']
            );
            $ret = $this->Creditor_model->verify($data);
            echo $ret ? '操作成功' : '操作失败';
        } else {
            echo '错误操作';
        }
    }

    /**
     * 撤标
     * @param $id
     */
    function withdraw($id) {
        $user = $this->session->userdata('zjy_user');
        $data['id'] = $id;
        $data['user_id'] = $user['user_id'];
        $data['ip'] = $this->input->ip_address();
        ;
        $this->load->model('Borrow_model');
        echo $this->Borrow_model->Cancel($data);
    }

    /**
     * 还款
     * @param $id
     */
    function repay($id) {
        $user = $this->session->userdata('zjy_user');
        $data['repayment_id'] = $id;
        $data['user_id'] = $user['user_id'];
        $data['ip'] = $this->input->ip_address();
         $this->load->model('Borrow_model');
          echo $this->Borrow_model->repay($data);
    }


    /**
     * 流转标还款
     * @param $id
     */
    function lz_repay($id) {
        $user = $this->session->userdata('zjy_user');
        $data['collection_id'] = $id;
        $data['user_id'] = $user['user_id'];
        $data['ip'] = $this->input->ip_address();
        $this->load->model('Borrow_model');
        echo $this->Borrow_model->lz_repay($data);
    }

    /**
     * 债权转让协议 html
     * @param int $borrow_id
     * @param int $tender_id
     */
    function creditor_protocol($creditor_id, $user_id, $id) {

        $creditor_id = intval($creditor_id);
        $this->load->model('Creditor_model');
        $this->load->model('Borrow_model');
        $this->load->model('User_model');

        //获取债权详情
        $creditor = $this->Creditor_model->get_obj_by_id($creditor_id);

        $borrow = $this->Borrow_model->get_borrow_obj($creditor['borrow_id'], array('select' => 1));

        $creditor_log = $this->Creditor_model->get_buyers_uid_by_id($creditor_id, $user_id, $id);

        $vdata['creditor_log'] = $creditor_log;

        $vdata['borrow'] = $borrow;

        $vdata['creditor'] = $creditor;
        $vdata['title'] = $creditor['title'] . ' - ' . CON_KEYWORDS;

        $this->load->view('/borrow/borrow_creditor_protocol', $vdata);
    }

    /**
     * 流转标协议书pdf
     * @param int $borrow_id
     * @param int $tender_id
     */
    function creditorprotocol($creditor_id) {
        $creditor_id = intval($creditor_id);
        if ($creditor_id > 0) {
            $this->load->library('func');
            $user = $this->session->userdata('zjy_user');
            $this->load->model('Creditor_model');
            $clog = $this->Creditor_model->get_by_log_id($creditor_id);
            $html = $this->func->protocol_msg_creditor($clog['obj_id'], $user['user_id'], $creditor_id);
            $this->load->library('/MPDF56/mpdf');
            $this->mpdf->useAdobeCJK = true;
            $this->mpdf->SetAutoFont(AUTOFONT_ALL);
            $this->mpdf->SetDisplayMode('fullpage');
            $this->mpdf->showWatermarkText = true;
            $this->mpdf->SetAutoFont();

            $this->mpdf->showWatermarkText = true;
            $this->mpdf->SetAutoFont();

            $this->mpdf->WriteHTML($html);
            $this->mpdf->Output();
        }
    }

}
